package cn.com.cfae.iras.doc.analyze.components.rationality;

import cn.com.cfae.iras.doc.analyze.components.BaseComponent;
import cn.com.cfae.iras.doc.analyze.components.beans.ComponentResult;
import cn.com.cfae.iras.doc.analyze.handUtil.HandingData;
import cn.com.cfae.iras.doc.analyze.parse.kv.KVText;
import cn.com.cfae.iras.doc.analyze.parse.model.ChapterModel;
import cn.com.cfae.iras.doc.analyze.parse.model.DocumentModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractBizModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractItem;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 内容合理性
 * M-3-1
 * 票据面值：检查是否为“100元”
 *
 * 发行阶段
 */
@Component("R1001")
@Scope("prototype")
public class R1001 extends BaseComponent {
    private static Logger log = LoggerFactory.getLogger(R1001.class);
    @Autowired
    private HandingData handingData;

    @Override
    public List<ComponentResult> handlerM() {
        log.info("获取R1001组件M");
        List<ComponentResult> componentResults = checkRulesR1001(getCatalogId(),getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBM() {
        log.info("获取R1001组件BM");
        List<ComponentResult> componentResults = checkRulesR1001(getCatalogId(),getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerDM() {
        log.info("获取R1001组件DM");
        List<ComponentResult> componentResults = checkRulesR1001(getCatalogId(),getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBDM() {
        log.info("获取R1001组件BDM");
        List<ComponentResult> componentResults = checkRulesR1001(getCatalogId(),getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }


    private List<ComponentResult> checkRulesR1001(String model,String catalogId,String chapterId,String ruleName){
        DocumentModel extractData = super.getExtractData();
        List<ComponentResult> componentResults = new ArrayList<>();
        ComponentResult componentResult = null;
        ExtractItem extractItem = null;
        int pageNum = 0;
        List<ComponentResult.TextPosition> contentAndPositionList = null;
        boolean flag = false;
        int startPage = 0;
        try {
            if (extractData!=null){
                //票据面值
                ChapterModel chapterModel = extractData.getChapterModel(model);
                startPage = chapterModel.getStartPage();
                extractItem = getExtractItem(extractData,model,"FXTK","PJMZ");
                if (extractItem!=null){
                    KVText value = extractItem.getValue();
                    KVText key = extractItem.getKey();
                    String text = value!=null?value.getText():null;
                    pageNum = value!=null?value.getPageNumber():startPage;
                    pageNum = pageNum!=0?pageNum:startPage;
                    contentAndPositionList = key!=null?getContentAndPositionList(key):null;
                    //String[] synonyms = extractItem.getSynonyms();
                    String[] synonyms = new String[]{"人民币壹佰元","100元","RMB100.00元","人民币100元","￥100","壹佰元"};
                    if (StringUtils.isNotEmpty(text)){
                        text = text.replaceAll("\\s","");
                        for (String synonym : synonyms){
                            if (text.contains(synonym)){
                                flag = true;
                                break;
                            }
                        }

                        if (flag){
                            componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"票据面值检查-是否为100元","合理",false,getRegulationId(), pageNum,contentAndPositionList,"01");
                            componentResults.add(componentResult);
                        }else {
                            componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"票据面值检查-是否为100元","票据面值,请人工核对",true,getRegulationId(), pageNum,contentAndPositionList,"02");
                            componentResults.add(componentResult);
                        }

                    }else {
                        componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"票据面值检查-是否为100元","未识别,建议人工核对",true,getRegulationId(), startPage,contentAndPositionList,"03");
                        componentResults.add(componentResult);
                    }

                }else {
                    componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"票据面值检查-是否为100元","未识别,建议人工核对",true,getRegulationId(), startPage,contentAndPositionList,"03");
                    componentResults.add(componentResult);
                }
            }

        }catch (Exception e){
            componentResults.clear();
            componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"票据面值检查-是否为100元","未识别,建议人工核对",true,getRegulationId(), startPage,contentAndPositionList,"03");
            componentResults.add(componentResult);
            log.error(e.getMessage(),e);
        }
        return  componentResults;
    }

    private ExtractItem getExtractItem(DocumentModel extractData,String chapter,String model,String key){
        ExtractItem extractItem  = null;
        if (extractData!=null){
            ChapterModel chapterModel = extractData.getChapterModel(chapter);
            if (chapterModel != null){
                ExtractBizModel extractMode = chapterModel.getModel(model);
                if (extractMode!=null){
                    extractItem = extractMode.getExtractItem(key);
                }
            }
        }
        return extractItem;
    }

}
